package com.aurora.source.home.fragment;

import android.os.Bundle;
import android.support.annotation.Nullable;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.aurora.source.R;
import com.aurora.source.base.BaseLazyFragment;
import com.aurora.source.entity.SysPermissionEntity;
import com.aurora.source.home.bean.ContactUsDataBean;
import com.aurora.source.home.bean.NewAreaBean;
import com.aurora.source.home.bean.NewCityBean;
import com.aurora.source.home.bean.NewProvinceBean;
import com.aurora.source.home.presenter.AreaSelectPresenter;
import com.aurora.source.home.presenter.ContactUsPresenter;
import com.aurora.source.home.view.IAreaSelectView;
import com.aurora.source.home.view.IContactUsView;
import com.aurora.source.util.GreenDaoHelper;
import com.aurora.source.util.SPUtils;
import com.aurora.source.widget.RxThreeSelectDialog;
import com.contrarywind.listener.OnItemSelectedListener;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 联系我们
 */
public class ContactUsFragment extends BaseLazyFragment<ContactUsPresenter> implements IContactUsView,IAreaSelectView {


    @BindView(R.id.et_phone)
    EditText etPhone;
    @BindView(R.id.tv_province)
    TextView tvProvince;
    @BindView(R.id.et_address)
    EditText etAddress;
    @BindView(R.id.et_fax)
    EditText etFax;
    @BindView(R.id.et_postcode)
    EditText etPostcode;
    @BindView(R.id.tv_modification)
    TextView tvModification;
    private String companyId;
    private String provinceId;
    private String cityId;
    private String areaId;
    AreaSelectPresenter areaSelectPresenter;
    RxThreeSelectDialog rxThreeSelectDialog;
    List<NewProvinceBean> provinceBeans;
    NewProvinceBean selectProvinceBean;
    List<NewCityBean> cityBeans;
    NewCityBean selectCityBean;
    List<NewAreaBean> areaBeans;
    NewAreaBean selectAreaBean;
    private boolean updateCompany;

    @Override
    public void doLazyBusiness() {
        refreshUI();
    }

    public void refreshUI(){
        Map<String, String> params = new HashMap<>();
        params.put("querySource", "0");
        params.put("userId", (String) SPUtils.get(activity, SPUtils.USER_ID, ""));
        basePresenter.ContactUsSource(activity, params);
    }

    @Override
    public void invisibleBusiness() {

    }

    @Override
    public ContactUsPresenter attach() {
        areaSelectPresenter = new AreaSelectPresenter(this);
        return new ContactUsPresenter(this);
    }

    @Override
    public int bindLayout() {
        return R.layout.fragment_contact_us;
    }

    @Override
    public void initData(@Nullable Bundle bundle) {
        SysPermissionEntity sysPermissionEntity = GreenDaoHelper.getDaoSession().getSysPermissionEntityDao().loadAll().get(0);
        updateCompany = sysPermissionEntity.getUpdateCompany();
        provinceBeans = new ArrayList<>();
        cityBeans = new ArrayList<>();
        areaBeans = new ArrayList<>();
    }

    @Override
    public void initView(Bundle savedInstanceState, View contentView) {
        if(updateCompany){
            etFax.setFocusable(true);
            etPhone.setFocusable(true);
            etPostcode.setFocusable(true);
            etAddress.setFocusable(true);
            tvProvince.setEnabled(true);
            tvModification.setVisibility(View.VISIBLE);
        }else {
            tvModification.setVisibility(View.GONE);
            etFax.setFocusable(false);
            etPhone.setFocusable(false);
            etPostcode.setFocusable(false);
            etAddress.setFocusable(false);
            tvProvince.setEnabled(false);
        }
        rxThreeSelectDialog = new RxThreeSelectDialog(activity);
        rxThreeSelectDialog.setOnCancelClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                rxThreeSelectDialog.dismiss();
            }
        });
        rxThreeSelectDialog.setOnSureClick(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                String provinName="";
                String cityName="";
                String areaName="";
                if(selectProvinceBean!=null){
                    provinName =  selectProvinceBean.getProvinceName();
                    provinceId = selectProvinceBean.getProvinceId();
                }

                if(selectCityBean!=null){
                    cityName = selectCityBean.getCityName();
                    cityId = selectCityBean.getCityId();
                }

                if(selectAreaBean!=null){
                    areaName = selectAreaBean.getAreaName();
                    areaId = selectAreaBean.getAreaId();
                }

                tvProvince.setText(String.format("%s %s %s", provinName, cityName, areaName));
                rxThreeSelectDialog.dismiss();
            }
        });
        rxThreeSelectDialog.setOnItem1SelectedListener(new OnItemSelectedListener() {//选择省
            @Override
            public void onItemSelected(int index) {
                selectProvinceBean = provinceBeans.get(index);
                areaSelectPresenter.getCity(activity,selectProvinceBean.getProvinceId());
            }
        });

        rxThreeSelectDialog.setOnItem2SelectedListener(new OnItemSelectedListener() { //选择市
            @Override
            public void onItemSelected(int index) {
                selectCityBean = cityBeans.get(index);
                areaSelectPresenter.getArea(activity,selectCityBean.getCityId());
            }
        });

        rxThreeSelectDialog.setOnItem3SelectedListener(new OnItemSelectedListener() {
            @Override
            public void onItemSelected(int index) {
                selectAreaBean = areaBeans.get(index);
            }
        });
    }



    @OnClick({R.id.tv_modification,R.id.ll_area_select})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_modification:
                if(!updateCompany){
                    showToast("暂无权限");
                    return;
                }
                Map<String,String> params = new HashMap<>();
                params.put("companyId",companyId==null?"":companyId);
                params.put("provinceId",provinceId==null?"":provinceId);
                params.put("cityId",cityId==null?"":cityId);
                params.put("areaId",areaId==null?"":areaId);
                params.put("companyStreet",etAddress.getText().toString());
                params.put("companyPhone",etPhone.getText().toString());
                params.put("companyPostalCode",etPostcode.getText().toString());
                params.put("companyFax",etFax.getText().toString());
                basePresenter.upDataCopInfo(activity,params);
                break;

            case R.id.ll_area_select:
                if(!updateCompany){
                    showToast("暂无权限");
                    return;
                }
                areaSelectPresenter.getProvince(activity);
                break;
        }
    }

    @Override
    public void onContactUsSuccess(ContactUsDataBean data) {
        companyId = data.getCompanyId();
        provinceId = data.getProvinceId();
        cityId =data.getCityId();
        areaId =data.getAreaId();
        etPhone.setText(data.getCompanyPhone()==null?"":data.getCompanyPhone());
        etAddress.setText(data.getCompanyStreet()==null?"":data.getCompanyStreet());
        etPostcode.setText(data.getCompanyPostalCode()==null?"":data.getCompanyPostalCode());
        etFax.setText(data.getCompanyFax()==null?"":data.getCompanyFax());
        tvProvince.setText(data.getCompanyAddress()==null?"":data.getCompanyAddress());
    }



    @Override
    public void onContactUsFail(String msg) {
        showToast(msg);
    }

    @Override
    public void onUpDataFail(String msg) {
        showToast(msg);
    }

    @Override
    public void onUpDataSuccess(String msg) {
        showToast(msg);
        refreshUI();
    }

    @Override
    public void onGetProvinceSuccess(List<NewProvinceBean> data) {
        provinceBeans.clear();
        provinceBeans.addAll(data);
        List<String> data1 = new ArrayList<>();
        for(int i=0;i<provinceBeans.size();i++){
            data1.add(provinceBeans.get(i).getProvinceName());
        }
         int currentPosition = rxThreeSelectDialog.setData1(data1);
        if(provinceBeans.size()>0){
            //默认选择第一个
            selectProvinceBean = provinceBeans.get(currentPosition);
            areaSelectPresenter.getCity(activity,selectProvinceBean.getProvinceId());
        }else {
            rxThreeSelectDialog.setData2(null);
            rxThreeSelectDialog.setData3(null);
        }
    }

    @Override
    public void onGetProvinceFail(String msg) {
        showToast(msg);
        rxThreeSelectDialog.setData1(null);
        rxThreeSelectDialog.setData2(null);
        rxThreeSelectDialog.setData3(null);
    }

    @Override
    public void onGetCitySuccess(List<NewCityBean> data) {
        cityBeans.clear();
        cityBeans.addAll(data);
        List<String> data2 = new ArrayList<>();
        for(int i=0;i<cityBeans.size();i++){
            data2.add(cityBeans.get(i).getCityName());
        }
        int currentPosition = rxThreeSelectDialog.setData2(data2);
        if(cityBeans.size()>0){
            //默认选择第一个
            selectCityBean = cityBeans.get(currentPosition);
            areaSelectPresenter.getArea(activity,selectCityBean.getCityId());
        }else {
            rxThreeSelectDialog.setData3(null);
        }

    }

    @Override
    public void onGetCityFail(String msg) {
        showToast(msg);
        rxThreeSelectDialog.setData2(null);
        rxThreeSelectDialog.setData3(null);
    }

    @Override
    public void onGetAreaSuccess(List<NewAreaBean> data) {
        areaBeans.clear();
        areaBeans.addAll(data);
        List<String> data3 = new ArrayList<>();
        for(int i=0;i<areaBeans.size();i++){
            data3.add(areaBeans.get(i).getAreaName());
        }
        int currentPosition = rxThreeSelectDialog.setData3(data3);
        selectAreaBean = areaBeans.get(currentPosition);
        if(!rxThreeSelectDialog.isShowing()){
            rxThreeSelectDialog.show();
        }
    }

    @Override
    public void onGetAreaFail(String msg) {
        showToast(msg);
        rxThreeSelectDialog.setData3(null);
    }
}
